package com.android.widget.horizontalscrollview;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.widget.demo.R;

public class HorizontalScrollViewAdapter extends ArrayAdapter<Map<String,Object>> {
    private Context mContext;
    
    public HorizontalScrollViewAdapter(Context context, int resource, List<Map<String,Object>> items) {
        super(context,resource,items);      
        mContext = context;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.horizontal_scroll_view_item,null);
            
            holder = new ViewHolder();
            holder.image = (ImageView)convertView.findViewById(R.id.image);
            holder.text = (TextView)convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        
        final Map<String,Object> map=getItem(position);
        holder.image.setBackgroundDrawable((Drawable)map.get("image"));
        holder.text.setText(map.get("text").toString());
        return convertView;
    }
    
    private static class ViewHolder {
        ImageView image;
        TextView text;
    }
}
